package com.example.firstapp;

import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyListActivity extends ListActivity implements Runnable, AdapterView.OnItemClickListener {
    Handler handler = new Handler();
    private static final String TAG="wwwww";
    private ListView list2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        List<String> list1 = new ArrayList<String>();
        for(int i = 1;i<100;i++){
            list1.add("item"+i);
        }
        //创建线程
        Thread t = new Thread(this);
        t.start();

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(Message msg){
                if(msg.what==2){
                    ArrayList<String>list2 = (ArrayList<String>) msg.obj;
                    Log.i("list", "handleMessage: "+list2);
                    ListAdapter adapter = new ArrayAdapter<String>(
                            MyListActivity.this,
                            android.R.layout.simple_expandable_list_item_1,
                            list2
                    );
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };

    }

    @Override
    public void run() {

//        URL url = null;
        SharedPreferences sp = getSharedPreferences("myrate", Activity.MODE_PRIVATE);
        List<String> retlist = new ArrayList<>();


        try {
            Document doc = Jsoup.connect("https://www.usd-cny.com/bankofchina.htm").get();
            Log.i("xx", "run: title :"+doc.title());
            Elements tables = doc.getElementsByTag("table");
            Element table1 = tables.first();
            Elements trs = table1.getElementsByTag("tr");
            trs.remove(0);
//获取table内的tr
            for(Element tr:trs){
                Elements tds = tr.getElementsByTag("td");
                String cname = tds.get(0).text();
                String cval = tds.get(5).text();
                Log.i(TAG,"run: cname="+cname+"-->"+cval);
                HashMap<String,String> map=new HashMap<String, String>();
                map.put("ItemTitle",cname);
                map.put("ItemDetail",cval);
                retlist.add(String.valueOf(map));
//                retlist.add(cname+"--->"+cval);
//                Log.i("xx", "run: cname"+cname+"---->"+"cval:"+cval  );
            }
            Message msg = handler.obtainMessage(2);
            msg.obj = retlist;
            handler.sendMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object itemAtPosition=list2.getItemAtPosition(position);
        HashMap<String,String> map=(HashMap<String, String>) itemAtPosition;
        String titleStr=map.get("ItemTitle");
        String detailStr=map.get("ItemDtetail");
        Log.i(TAG,"onItemClick: titleStr="+titleStr);
        Log.i(TAG,"onItemClick: detailStr="+detailStr);
        TextView title=(TextView) view.findViewById(R.id.list_item);
        TextView detail=(TextView) findViewById(R.id.text);
        String title2=String.valueOf(title.getText());
        String detail2=String.valueOf(detail.getText());
        Log.i(TAG,"onItemClick: title="+title2);
        Log.i(TAG,"onItemClick: detail2="+detail2);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}